<template>
  <div class="backStack-issue-wrap informationCreate">
    <From
      ref="formRef"
      layoutType="grid"
      :isErrorMessage="false"
      :optionList="formRows"
      :formRules="formRules"
      :defaultModel="defaultModel"
    >
    </From>
    <div class="backStack-issue-footer-wrap">
      <div class="submit-wrap">
        <a-button class="submit-audit" @click="toAudit(0)">保存</a-button>
        <a-button class="submit-audit" @click="toAudit(1)">保存并发布</a-button>
      </div>
    </div>
  </div>
</template>

<script>
import From from '../../components/formTable/form.vue'
import mixinsBackStack from '@/mixins/backStack'
import mixinConfigSetting from '@/mixins/mixinConfigSetting'
import mixinFakeData from '@/mixins/makeFakerData'
import { formRules, formRows, counseling, policy } from './configs/formOptions.js'
import { issueDataStatistics } from '@/api/dataStatistics'

export default {
  mixins: [mixinsBackStack, mixinConfigSetting, mixinFakeData],
  data() {
    return {
      loading: true,
      formRows, // 表单item配置
      formRules, // 表单校验规则配置
      counseling,
      policy,
      defaultModel: {},
      isRead: false
    }
  },
  components: {
    From
  },
  created() {
  },
  methods: {
    readChange(e) {
      const { target } = e
      this.isRead = target.checked
    },
    async toAudit(status) {
      const v = this.$refs.formRef.validatedForm()
      if (!v) return
      try {
        const { title, content, pulishDate, source, ...formValue } = this.$refs.formRef.formModel
        const annexIds = this.handlerAnnexIds(formValue).join(',')
        const params = {
          content,
          annexIds,
          title,
          status,
          pulishDate,
          source
        }
        const { msg, code } = await issueDataStatistics(params)
        this.$apiStatusHandler(code, msg)
        this.$router.push({ name: 'contentDataStatistics' })
      } catch (error) {
        console.log(error)
        this.$apiStatusHandler(99, error)
      }
    },
    handlerAnnexIds(instance) {
      const { annexIds } = instance
      if (!annexIds) return []
      return annexIds.reduce((pre, item) => {
        const { fileId } = item.response.data[0]
        pre.push(fileId)
        return pre
      }, [])
    },
  }
}
</script>
